<template>
  <div>
    <el-table :data="tableData" style="width: 100%">
      <el-table-column label="日志Id" width="250">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.row._id }}</span>
        </template>
      </el-table-column>
      <el-table-column label="登录管理员" width="150">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{
            scope.row.adminsId.username
          }}</span>
        </template>
      </el-table-column>
      <el-table-column label="登录时间" width="300">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.row.time }}</span>
        </template>
      </el-table-column>
      <el-table-column>
        <template slot="header" slot-scope="scope">
          <div id="search">
            <el-input
              placeholder="请输入内容"
              v-model="input"
              class="input-with-select"
            >
              <el-select v-model="select" slot="prepend" placeholder="请选择">
                <el-option label="姓名" value="username"></el-option>
                <el-option label="登录时间" value="time"></el-option>
              </el-select>
              <el-button
                slot="append"
                icon="el-icon-search"
                @click="sousuo"
              ></el-button>
            </el-input>
          </div>
        </template>
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.$index, scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <div class="block">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[5, 10, 20, 50]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
import apis from "../../http/apis/apis";
export default {
  data() {
    return {
      tableData: [
        {
          _id: 978766534,
          adminsId: { username: "王小芳" },
          time: "2070-11-20 20:30:40",
        },
      ],
      select: "",
      input: "",
      currentPage: 1,
      pageSize: 5,
      total: 100,
    };
  },
  mounted() {
    this.getLogs();
  },
  methods: {
    //获取日志列表
    async getLogs() {
      const data = await apis.logs.getLogs({
        select: this.select,
        input: this.input,
        currentPage: this.currentPage,
        pageSize: this.pageSize,
      });
      console.log(data)
      if (data.data.status) {
        if (this.select == "username") {
          this.tableData = data.data.data.data.filter((item) => {
            return item.adminsId.username.includes(this.input);
          });
        } else {
          this.tableData = data.data.data.data;
          this.total = data.data.data.total;
        }
      }
    },
    //删除日志
    async handleDelete(index, row) {
      const adminsId = row.adminsId._id;
      const { _id, time } = row;
      const state = 2;
      const data = await apis.logs.setLogs({ adminsId, _id, time, state });
      this.getLogs()
    },
    //搜索
    sousuo() {
      console.log("sou", this.select, this.input);
      this.getLogs();
    },
    //每页显示条数
    handleSizeChange(val) {
      this.pageSize = val;
      this.getLogs();
    },
    //当前页数
    handleCurrentChange(val) {
      this.currentPage = val;
      this.getLogs();
    },
  },
};
</script>

<style lang="scss" scoped>
#search {
  width: 400px;
  .el-select {
    width: 100px !important;
  }
}
</style>